package com.three_learn.star.system.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.three_learn.star.system.entity.sys_dept;
import com.three_learn.star.system.mapper.DeptMapper;
import com.three_learn.star.system.po.DTO.DepDTO;
import com.three_learn.star.system.po.VO.DepVO;
import com.three_learn.star.system.service.IDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/*
 *描述 ：
 *Author:28768
 */
@Service
public class IDeptServiceImpl extends ServiceImpl<DeptMapper, sys_dept> implements IDeptService {

     @Resource
     private DeptMapper deptMapper;

     //查找部门列表
     @Override
     public List<DepVO> getList(DepDTO depDTO) {
          LambdaQueryChainWrapper<sys_dept> wrapper = lambdaQuery()
                  .like(depDTO.getName()!=null,sys_dept::getName, depDTO.getName())
                  .eq(depDTO.getCode()!=null,sys_dept::getCode, depDTO.getCode())
                  .eq(depDTO.getLeader()!=null,sys_dept::getLeader, depDTO.getLeader())
                  .eq(depDTO.getStatus()!=null,sys_dept::getStatus, depDTO.getStatus());
          //使用wrapper查询sys_dept里的数据
          List<sys_dept> sysDepts = wrapper.list();
          return  sysDepts.stream().map((sysDept)->{
               DepVO depVO = new DepVO();
               BeanUtils.copyProperties(sysDept,depVO);
               return depVO;
          }).collect(Collectors.toList());
     }
     //添加部门
     @Override
     public void add(DepDTO depDTO) {
          sys_dept sysDept = new sys_dept();
          BeanUtils.copyProperties(depDTO,sysDept);
          //创建时间
          sysDept.setCreateTime(LocalDateTime.now());
          //更新时间
          sysDept.setUpdateTime(LocalDateTime.now());
          //TODO 没有添加用户信息 level TenantId
          sysDept.setLevel(9);
          sysDept.setTenantId(99999L);
          deptMapper.insert(sysDept);
     }
     //根据id删除部门
     @Override
     public boolean deleteById(Long id) {
          //查出id的数据
          sys_dept sysDept = deptMapper.selectById(id);
          //level>当前，同时ancestores包含查出数据的ancestores
          String ancestorss = sysDept.getAncestors();
          if(sysDept.getAncestors()!=null){
               //level>当前
               List<sys_dept> sysDepts = lambdaQuery()
                       .gt(sys_dept::getLevel, sysDept.getLevel())
                       .like(sys_dept::getAncestors,ancestorss)
                       .list();
               //判断能否删除
               if(sysDepts.stream().anyMatch((sysDept1) -> sysDept1.getAncestors().contains(ancestorss)&&sysDept1.getAncestors().length()>ancestorss.length()))
               {
                    return false;
               }
          }
          //满足条件进行逻辑删除
          deptMapper.deleteById(id);
          return true;
     }
     //更新数据
     @Override
     public void update(DepDTO depDTO) {
          sys_dept sysDept = deptMapper.selectById(depDTO.getId());
          BeanUtils.copyProperties(depDTO,sysDept);
          sysDept.setUpdateTime(LocalDateTime.now());
          deptMapper.updateById(sysDept);
     }
     //分页查询部门
     @Override
     public List<DepVO> getPageQuery(Integer page, Integer pageSize) {
          //分页基本条件
          Page<sys_dept> pageQuery=new Page<>(page,pageSize);
          //第二个参数是查询条件
          Page<sys_dept> sysDeptPage = deptMapper.selectPage(pageQuery, null);
          return sysDeptPage.getRecords().stream().map(dept->{
               DepVO depVO = new DepVO();
               BeanUtils.copyProperties(dept,depVO);
               return depVO;
          }).collect(Collectors.toList());
     }


}
